﻿using Accouting804.Services;
using Accouting804.Setting;
using Accouting804.Views;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Text;
using Xamarin.Forms;

namespace Accouting804.ViewModels.AccountViewModels
{
    public class AccountsBaseViewModel : INotifyPropertyChanged
    {

        public ObservableCollection<Category> Categories { get; set; }
        public ObservableCollection<PaymentMethod> PaymentMethods { get; set; }
        public ObservableCollection<Book> Books { get; set; }

        public AccountsBaseViewModel()
        {
            CategoryDataStore.GetItemsAsync().ContinueWith((task) => Categories = new ObservableCollection<Category>(task.Result));
            PaymentMethodDateStore.GetItemsAsync().ContinueWith((task) => PaymentMethods = new ObservableCollection<PaymentMethod>(task.Result));
            BookDataStore.GetItemsAsync().ContinueWith(task => Books = new ObservableCollection<Book>(task.Result));
            MessagingCenter.Subscribe<AccountItemDetailPage, AccountRecord>(this, "UpdateAccountItem", async (obj, item) =>
            {
                var newItem = item as AccountRecord;
                await AccountDataStore.UpdateItemAsync(newItem);


                ;
                //await AccountDataStore.AddItemAsync(newItem);
            });
        }

        #region Properties
        public IDataStoreWithFilterQuery<AccountRecord> AccountDataStore => DependencyService.Get<IDataStoreWithFilterQuery<AccountRecord>>();
        public IDataStoreWithFilterQuery<Category> CategoryDataStore => DependencyService.Get<IDataStoreWithFilterQuery<Category>>();
        public IDataStoreWithFilterQuery<Book> BookDataStore => DependencyService.Get<IDataStoreWithFilterQuery<Book>>();

        public IDataStoreWithFilterQuery<PaymentMethod> PaymentMethodDateStore => DependencyService.Get<IDataStoreWithFilterQuery<PaymentMethod>>();
        #endregion

        #region Fields
        protected bool isBusy = false;
        protected string title = string.Empty;
        #endregion
        public bool IsBusy
        {
            get { return isBusy; }
            set { SetProperty(ref isBusy, value); }
        }

        public string Title
        {
            get { return title; }
            set { SetProperty(ref title, value); }
        }



        protected bool SetProperty<T>(ref T backingStore, T value, [CallerMemberName]string propertyName = "", Action onChanged = null)
        {
            if (EqualityComparer<T>.Default.Equals(backingStore, value))
                return false;
            backingStore = value;
            onChanged?.Invoke();
            OnPropertyChanged(propertyName);
            return true;
        }

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged([CallerMemberName] string propertyName = "")
        {
            var changed = PropertyChanged;
            if (changed == null)
                return;

            changed.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
        #endregion
    }

}
